#!/usr/bin/env python
# coding:utf-8

import commands
import json
import string
import time
import os
import sys

reload(sys)
sys.setdefaultencoding('utf8')
import pexpect
from pexpect import pxssh
LOG = ""
HOST_LIST = [
    '10.161.10.190',
    '10.161.10.191',
    '10.161.10.192',
    '10.161.10.193',
    '10.161.10.182',
    '10.161.10.189',
    '10.161.10.47',
    '10.161.10.48',
    '10.161.70.217',
    '10.161.70.218'
]
PROVINCE_NAME = [
    ["����", "����", "����", "����"],
    ["������", "�㽭  ", "���  ", "�Ϻ�  ", "����  "],
    ["�㶫", "�Ĵ�", "����", "����"],
    ["����", "����", "�½�", "����"],
    ["����", "���ɹ�", "����", "����"],
    ["ɽ��", "�ຣ"],
    ["�ӱ�", "����", "����"],
    ["ɽ��", "����", "����", "����", "����"]
];
SHOULD = [-1,1000,2000,3000,4000,5000,6000,7000,8000,9000]

START = []
ONE_DOMAIN = []
ONE_PROVINCE = []

class CheckContinuity(object):
    username = ''
    passwd = ''
    log_time = ''
    final_reslut = ''
    active_reslut = []

    def __init__(self):
        self.username = 'billing'
        self.passwd = 'U1n@i3v$e5r'
        self.path = '/cbss/billing/log/'
        self.log_time = time.strftime('%Y%m', time.localtime(time.time()))

    def ssh_cmd(self, ip, cmd):
        resLsit = []
        ssh = pexpect.spawn('ssh %s@%s "%s"' % (self.username, ip, cmd))
        try:
            #            i = ssh.expect(['password:', 'continue connecting (yes/no)?', ']'], timeout=10)
            #            if i == 0:
            #                ssh.sendline(self.passwd)
            #            elif i == 1:
            #                ssh.sendline('yes\n')
            #                ssh.expect('password: ')
            #                ssh.sendline(self.passwd)
            #            else:
            #                pass
            ssh.send(cmd + '\n')
#            print(ssh.read())
            resLsit = ssh.read().split('\r\n')
            ret = 0
        except pexpect.EOF:
            print("EOF error")
            ssh.close()
            resLsit.append(-1)
        except pexpect.TIMEOUT:
            print("Timeout error")
            ssh.close()
            resLsit.append(-2)
        return resLsit

    def getRelatedLogs(self, logString, host, i, j):
        cmd = 'grep \'' + logString + '\' /cbss/billing/log/Meventor.' + self.log_time + '* |grep gboss.cbs.billing.Meventor.' + str(i) + str(j)
#        print(cmd)
        list = self.ssh_cmd(host, cmd)
#        print(list)
        for oneSql in list:
            if oneSql.find('>=') == -1:
                continue
	    s = oneSql.find('>=')
	    s1 = oneSql.find('< ')
 #           print(oneSql[s + 2:oneSql.find(' ',s+4)])
 #           print(oneSql[s1 + 2:oneSql.find(' ',s1+3)])
            a = oneSql[s + 2:oneSql.find(' ',s+4)]
            b = oneSql[s1 + 2:oneSql.find(' ',s1+3)]
#            print(eval(a))
	    ONE_PROVINCE.append(eval(a))
	    a = eval(a)
	    if a == -1:
		a=0
	    while 1:
		if a+1000<eval(b):
		    a +=  1000
		    ONE_PROVINCE.append(a)
		else:
		    break;
		
#	    print(ONE_PROVINCE)


if __name__ == '__main__':
    zhangqi = sys.argv[1]
    if zhangqi is null:
        print("null")
    cc = CheckContinuity()
    i = 1
    j = 1
    for domain in PROVINCE_NAME:
        for province in domain:
            print('\n��ʼ���'+province)
	    LOG+='\n��ʼ���'+province+'\n'
            for oneHost in HOST_LIST:
                cc.getRelatedLogs("""Select USER_ID, SERIAL_NUMBER, EPARCHY_CODE, OPEN_DATE
  From tf_f_user a
 Where a.DUMMY_TAG = '0'
   And a.acct_tag = '0'
   And (a.remove_tag = '0' Or
       (a.remove_tag != '0' And
       nvl(DESTROY_DATE,
             nvl(PRE_DESTROY_DATE,
                 to_date('20500101000000', 'YYYYMMDDHH24MISS'))) >
       to_date('20191201000000', 'YYYYMMDDHH24MISS')))
   AND a.open_date <= to_date('20191231235959', 'YYYYMMDDHH24MISS')
   and a.PROVINCE_CODE = '11'""", oneHost,i,j)
            j+=1
	    ONE_DOMAIN.append(ONE_PROVINCE)
#	    print(ONE_DOMAIN)
	    for k in SHOULD:
		if k not in ONE_PROVINCE:
		    print('ȱ��'+str(k)+' - '+str(k+1000))
		    LOG+='ȱ��'+str(k)+' - '+str(k+1000)+'\n'
	    ONE_PROVINCE = []
        i+=1
        j=1
	START.append(ONE_DOMAIN)
#	print(START)
	ONE_DOMAIN = []
    with open('U_result.txt','w+') as o:
    	o.write(str(START))
    with open('U_result.log','w+') as o:
	o.write(LOG)
